package com.zgt.dao.impl;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.zgt.dao.StudentDao;
import com.zgt.model.Admin;
import com.zgt.model.Student;

@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void deleteStudentById(int id) {
		Student student = new Student();
		student.setId(id);
		hibernateTemplate.delete(student);
	}

	public Student getStudentById(int id) {
		return hibernateTemplate.get(Student.class, id);
	}

	public void saveStudent(Student student) {
		hibernateTemplate.save(student);
	}

	public void updateStudent(Student student) {
		hibernateTemplate.update(student);
	}

	public Student checkLogin(Student student) {
		Student s = null;
		List<Student> students = hibernateTemplate.findByExample(student);
		Iterator<Student> istudents = students.iterator();
		while (istudents.hasNext()) {
			s = istudents.next();
		}
		// 返回持久化的对象
		return s;
	}

	public boolean isExist(Student Student) {
		// TODO Auto-generated method stub
		return false;
	}

	public int getCount() {
		List<Student> students = hibernateTemplate.find("from Student");
		return students.size();
	}

	public List<Student> listStudent(int pageStart, int pageSize) {
		List<Student> students = hibernateTemplate.findByCriteria(
				DetachedCriteria.forClass(Student.class), pageStart, pageSize);
		return students;
	}

	public void saveStudents(List list) {
		Iterator it = list.iterator();
		while (it.hasNext()) {
			hibernateTemplate.save(it.next());
		}
	}

	@SuppressWarnings("unchecked")
	public List<Student> toExcle(final String keyType, final String key) {
		List<Student> students = hibernateTemplate
				.executeFind(new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Criteria query = session.createCriteria(Student.class);
						if (!key.isEmpty()) {
							query.add(Restrictions.like(keyType, "%" + key
									+ "%"));
						}
						return query.list();
					}
				});
		return students;
	}

	public int getCount(String query) {
		List<Student> students = hibernateTemplate.find(query);
		return students.size();
	}
	
	public List<Student> query(String query) {
		List<Student> students = hibernateTemplate.find(query);
		return students;
	}

	public List<Student> listSearch(final String query, final int start,
			final int size) {
		List<Student> students = hibernateTemplate
				.executeFind(new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.createQuery(query).setFirstResult(start)
								.setMaxResults(size).list();
					}
				});
		return students;
	}
}
